package test;

public class Stack02 {
    String[] array;
    int max;
    int topPointer=-1;//栈针----指向栈顶元素
    //初始化容量
    public Stack02(int max){
        this.max=max;
        this.array=new String[max];
    }
    //入栈
    public void push(String data){
        if(topPointer==max-1){
            throw new RuntimeException("栈已满");
        }
        topPointer++;
        array[topPointer]=data;
    }
    //出栈
    public String pop(){
        if(topPointer==-1){
            throw new RuntimeException("栈已空");
        }
        String value = array[topPointer];
        array[topPointer]=null;
        topPointer--;
        return value;
    }
    //返回栈顶元素，不弹栈
    public String peek(){
        return array[topPointer];
    }
}
